<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib tagdir="/WEB-INF/tags/sys" prefix="sys"%>
<%@ taglib uri="/WEB-INF/tlds/webui.tld" prefix="webui"%>
<%--内容分类 --%>
<webui:enum var="contentType" code="contentType" />
<%--是否独家 --%>
<webui:enum var="exclusive" code="exclusive" />

<webui:panel id="editCpContent" style="display:none;" title="编辑内容">
	<webui:form method="post" styleClass="form-horizontal"
		id="editCpContentForm">

		<input type="hidden" id="editFromContentId" name="id" />

		<div class="col-sm-12">
			<webui:select label="影片分类" name="contentType"
				id="editFromContentType" styleClass="col-sm-6" inputClass="col-sm-8"
				labelClass="col-sm-4" lookup="${contentType}">
				<webui:validator required="true" />
			</webui:select>

			<webui:select label="是否独家" name="exclusive" id="editFromExclusive"
				styleClass="col-sm-6" inputClass="col-sm-8" labelClass="col-sm-4"
				lookup="${exclusive}">
				<webui:validator required="true" />
			</webui:select>
		</div>

		<div class="col-sm-12" id="series">
			<webui:select label="地区" name="area" id="editFromArea"
				styleClass="col-sm-6" inputClass="col-sm-8" labelClass="col-sm-4">
				<webui:validator required="true" />
			</webui:select>

			<webui:select label="类型" name="type" id="editFromType"
				styleClass="col-sm-6" placeholder="请选择类型，最少三个最多五个"
				selectClass="chosen-select" inputClass="col-sm-8"
				labelClass="col-sm-4" multiple="true">
				<webui:validator required="true" />
			</webui:select>
		</div>

		<div class="col-sm-12">
			<webui:input label="编号" id="editFromCode" name="code"
				styleClass="col-sm-6" placeholder="请输入格式为：Program_数字   的编号"
				type="text" inputClass="col-sm-8" labelClass="col-sm-4">
				<webui:validator maxlength="255"/>
			</webui:input>

			<webui:input label="外部编码" id="editFromExternalCode"
				name="externalCode" styleClass="col-sm-6" placeholder="请输入32位的数字编码"
				type="text" inputClass="col-sm-8" labelClass="col-sm-4">
				<webui:validator digits="true" minlength="32" maxlength="32">{'digits':'请输入32位的数字编码','minlength':'请输入32位的数字编码','maxlength':'请输入32位的数字编码'}</webui:validator>
			</webui:input>
		</div>

		<div class="col-sm-12">
			<webui:input label="片名" id="editFromName" name="name"
				styleClass="col-sm-6" placeholder="请输入片名" type="text"
				inputClass="col-sm-8" labelClass="col-sm-4">
				<webui:validator required="true" maxlength="255"/>
			</webui:input>

			<webui:input label="导演" id="editFromDirector" name="director"
				styleClass="col-sm-6" placeholder="多个导演姓名中间使用英文半角逗号分隔" type="text"
				inputClass="col-sm-8" labelClass="col-sm-4">
				<webui:validator required="true" maxlength="255"/>
			</webui:input>
		</div>

		<div class="col-sm-12">
			<webui:input label="演员" id="editFromActor" name="actor"
				styleClass="col-sm-6" placeholder="多个演员姓名中间使用英文半角逗号分隔" type="text"
				inputClass="col-sm-8" labelClass="col-sm-4">
				<webui:validator required="true" maxlength="255"/>
			</webui:input>

			<webui:input label="年代" id="editFromYears" name="years"
				styleClass="col-sm-6" placeholder="请输入年代" type="text"
				inputClass="col-sm-8" labelClass="col-sm-4">
				<webui:validator required="true" digits="true">{'digits':'年代格式不正确'}</webui:validator>
			</webui:input>
		</div>

		<div class="col-sm-12">
			<webui:input label="评分" id="editFromRating" name="rating"
				styleClass="col-sm-6" placeholder="请输入数字在1~10之间的评分" type="text"
				inputClass="col-sm-8" labelClass="col-sm-4">
				<webui:validator number="true">{'number':'评分格式不正确，请输入1~10之间的数字，且小数点最多保留1位'}</webui:validator>
			</webui:input>

			<webui:input label="集数" id="editFromEpisodeNum" name="episodeNum"
				styleClass="col-sm-6" placeholder="请输入集数" type="text"
				inputClass="col-sm-8" labelClass="col-sm-4">
				<webui:validator required="true" digits="true" max="10000">{'digits':'集数格式不正确'}</webui:validator>
			</webui:input>
		</div>

		<div class="col-sm-12">
			<webui:input label="简介" id="editFromIntro" name="intro"
				styleClass="col-sm-12" placeholder="请将字数限制在200字以内" type="textarea"
				inputClass="col-sm-10" labelClass="col-sm-2">
				<webui:validator maxlength="200">{'maxlength':'不能超过200位'}</webui:validator>
			</webui:input>
		</div>

		<div id="editCpContentSeries">
			<div class="col-sm-12">
				<webui:input label="跳过开头" id="editFromStartTime" name="startTime"
					styleClass="col-sm-6" placeholder="00:00:00" type="text"
					inputClass="col-sm-8" labelClass="col-sm-4">
				</webui:input>

				<webui:input label="跳过结尾" id="editFromEndTime" name="endTime"
					styleClass="col-sm-6" placeholder="00:00:00" type="text"
					inputClass="col-sm-8" labelClass="col-sm-4">
				</webui:input>
			</div>

			<div class="col-sm-12">
				<webui:input label="备注" id="editFromRemarks" name="remarks"
					styleClass="col-sm-12" placeholder="请将字数限制在200字以内" type="textarea"
					inputClass="col-sm-10" labelClass="col-sm-2">
					<webui:validator maxlength="200" >{'maxlength':'不能超过200位'}</webui:validator>
				</webui:input>
			</div>
		</div>
	</webui:form>
	<div class="row">
		<div class="col-md-7" style="float: right;">
			<webui:button type="button" buttonClass="btn-white"
				onclick="cancelCpContent()">取消</webui:button>
			<webui:button id="contentSaveBtn" type="button"
				buttonClass="btn-primary" ladda="true" onclick="saveContent()">保存</webui:button>
		</div>
	</div>
</webui:panel>

<script>
	$(function() {
		//删除影片分类中第一个option（请选中影片分类）
		$("#editFromContentType option:first").remove();
		$("#editFromContentType").change(function() {
			var select = $("#editFromContentType").val();
			vodOrSeries(select, 'Area', '#editFromArea');
			vodOrSeries(select, 'Type', '#editFromType');
		});
		
		$("#editFromRating").blur(function(){
			onCheckRating(this);
		});
		$("#editFromCode").blur(function() {
			onCheckCode(this);
		});
		$("#editFromExternalCode").blur(function() {
			onCheckExternalCode(this);
		});
		$("#editFromStartTime").blur(function() {
			onCheckTime(this);
		});
		$("#editFromEndTime").blur(function() {
			onCheckTime(this);
		});
		$("#editFromYears").blur(function() {
			onCheckYears(this);
		});
	});
	
	window.onload=function(){
		$("#editFromExternalCode").keyup(function(){
			var externalCode = $("#editFromExternalCode").val();
			if(externalCode.length > 32){
				$("#editFromExternalCode").val(externalCode.substring(0,32));
			}
		});
	}

	//取消
	function cancelCpContent() {
		grid_cpContent.show();
		$("#editFromName").attr("readonly",false);
		$("#editFromExclusive").attr("disabled",false);
		$("#editCpContentForm").reset();
		editCpContentFormValidator.resetForm();
	}
	//保存
	function saveContent() {
		//隐藏ID为空时保存，否则修改
		if (returnCheck()&&editCpContentFormValidator.form()) {
			var url = '${contextPath}/declare/cpContent/save.action';
			if($("#editFromContentId").val() != ""){
				url = "${contextPath}/declare/cpContent/edit.action?rowId="+$("#editFromContentId").val();
			}
			contentSaveBtn.ladda('start');
			$.ajax({
				method : "post",
				url : url,
				dataType : "json",
				contentType : "application/json; charset=utf-8",
				data : $('form#editCpContentForm').serializeJSON(),
				success : function(data) {
					contentSaveBtn.ladda('stop');
					var message = "";
					if(data.result==0){
						message = $("#editFromContentId").val() == "" ? '保存成功。': '修改成功。';
						showMessage('提示',message, function(){
							$('#editCpContentForm').reset();
							grid_cpContent.refresh();
							$("#editFromName").attr("readonly",false);
							$("#editFromExclusive").attr("disabled",false);
						});
					}else{
						message = $("#editFromContentId").val() == "" ? '保存失败。':'修改失败。';
						showError('提示',message+"，错误信息："+data.message, function() {
							$('#editCpContentForm').reset();
							grid_cpContent.refresh();
							$("#editFromName").attr("readonly",false);
							$("#editFromExclusive").attr("disabled",false);
						});
					}
				}
			});
		}
	}

	/*************************************************分类联动******************************************************/
	//影片分类的地区和类型联动
	function vodOrSeries(value, type, dom) {
		if (value != ""){
			if ($("#editFromContentType").val() != '') {
				var url = '${contextPath}/enum/listSelect.action?code=' + value
						+ type;
				onloadSelect(dom, url, '');
			}
		}
		if (value == "vod") {
			$("#editFromEpisodeNum").val(1).attr("readonly", true);
		} else {
			$("#editFromEpisodeNum").val("").attr("readonly", false);
		}
	}
	
	//同步的select联动加载事件
	//使用同步的原因：在修改时，必须要在加载完地区时，才能进行类型选择
	function onloadSelect(id, url, initVal) {
		var dataLinkIdOpt = $(id + " option:first");
		$(id).empty();

		if (($(id).attr("multiple") == undefined))
			$(id).append(dataLinkIdOpt);
		$.ajax({
			type : "GET",
			async : false,
			url : url,
			success : function(data) {
				$(data).each(function() {
					var opt = $('<option>', {
						value : this.code
					}).text(this.name);
					$(id).append(opt);
				});
				if (initVal) {
					$(id).val(initVal);
				}
				if ($(id).hasClass("chosen-select")) {
					$(id).trigger("chosen:updated");
				}
			}
		});
	}
	/*************************************************分类联动******************************************************/
	
	/*************************************************字段校验******************************************************/
	
	//非空验证
	function onNotNull(id,message){
		var value = $(id).val();
		if(value==''){
			outCheckError("error",message, id);
			return false;
		}
		outCheckError("correct", "", id);
		return true;
	}
	
	//验证类型选择了几个
	function onCheckContentTypeNumber() {
		//得到影片类型选择了几个
		var ContentTypeNumber = $("#editFromType_chosen .chosen-choices .search-choice").length;
		if (ContentTypeNumber<3 || ContentTypeNumber>5) {
			$("#editFromType_chosen").next().html("类型最少选择三个，最多选择五个");
			$("#editFromType_chosen").next().css("display","block");
			return false;
		} else {
			$("#editFromType_chosen").next().html("");
			$("#editFromType_chosen").next().css("display","none");
			return true;
		}
	}
	//验证code
	function onCheckCode(id) {
		var reg = /^[P][r][o][g][r][a][m][_]\d+$/;
		var code = $(id).val();
		if(code != ''){
			if (reg.test(code)) {
				var check = true;
				var conId = $("#editFromContentId").val() == "" ? "" : $("#editFromContentId").val();
				var url = '${contextPath}/declare/cpContent/findByCode.action?code='+code+'&conId='+conId
				$.ajax({
					url:url,
					type:'get',
					async : false,
					success : function(data){
						if(data){
							outCheckError("error", "该编码已经存在", id);
							check = false;
						}else{
							outCheckError("correct", "", id);
							check = true;
						}
					}
				});
				return check;
			} else {
				outCheckError("error", "请输入格式为：Program_+数字  的编号", id);
				return false;
			}
		}else{
			outCheckError("correct", "", id);
			return true;
		}
	}
	//验证externalCode
	function onCheckExternalCode(id) {
		var externalCode = $(id).val();
		if(externalCode != ''){
			var conId = $("#editFromContentId").val() == "" ? "" : $("#editFromContentId").val();
			var url = '${contextPath}/declare/cpContent/findByExternalCode.action?externalCode='+externalCode+'&conId='+conId;
			var check = true;
			$.ajax({
				url : url,
				type : 'get',
				async : false,
				success : function(data){
					if(data){
						outCheckError("error", "该外部编码已经存在", id);
						check = false;
					}else{
						outCheckError("correct", "", id);
						check = true;
					}
				}
			});
			return check;
		}else{
			outCheckError("correct", "", id);
			return true;
		}
	}
	//验证年代
	function onCheckYears(id) {
		var date = new Date().getFullYear();
		var years = $(id).val();
		var reg = /^[1-9]\d*$/;
		if(!reg.test(years)){
			outCheckError("error","年代格式不正确", id);
			return false;
		}else{
			if(years > date){
				outCheckError("error","不得大于当前年份", id);
				return false;
			}
			if(years < 1900){
				outCheckError("error","年份必须大于1900年", id);
				return false;
			}
		}
		outCheckError("correct", "", id);
		return true;
	}
	//验证评分
	function onCheckRating(id){
		var reg = /^(\d|10)(\.\d)?$/;
		var rating = $(id).val();
		if(rating!=''){
			if(!reg.test(rating)){
				outCheckError("error", "评分格式不正确，请输入1~10之间的数字，且小数点最多保留1位", id);
				return false;
			}
			if(rating > 10){
				outCheckError("error", "请输入1~10之间的数字，且小数点最多保留1位", id);
				return false;
			}
			if(rating < 1){
				outCheckError("error", "请输入1~10之间的数字，且小数点最多保留1位", id);
				return false;
			}
		}
		outCheckError("correct", "", id);
		return true;
	}
	//跳过开头和结尾的时间格式验证
	function onCheckTime(id) {
		var reg = /^([0-9][0-9]):([0-5][0-9]):([0-5][0-9])$/;
		var time = $(id).val();
		if (time != "") {
			if (reg.test(time)) {
				outCheckError("correct", "", id);
				return true;
			} else {
				outCheckError("error", "请输入规范的时间格式如00:00:00", id);
				return false;
			}
		} else {
			outCheckError("correct", "", id);
			return true;
		}
	}
	//错误提示
	function outCheckError(state, message, id) {
		if (state == "error") {
			$(id).addClass("error");
			$(id).next().html(message).css("display", "block");
		} else if (state == "correct") {
			$(id).removeClass("error");
			$(id).next().html(message).css("display", "none");;
		}
	}
	//提交校验
	function returnCheck() {
		var check = true;
		if(!onNotNull("#editFromExclusive","是否独家不能为空")){
			check = false;
		}
		if(!onNotNull("#editFromArea","地区不能为空")){
			check = false;
		}
		if(!onNotNull("#editFromName","片名不能为空")){
			check = false;
		}
		if(!onNotNull("#editFromDirector","导演不能为空")){
			check = false;
		}
		if(!onNotNull("#editFromActor","演员不能为空")){
			check = false;
		}
		if(!onNotNull("#editFromEpisodeNum","集数不能为空")){
			check = false;
		}
		if(!onCheckContentTypeNumber()){
			check = false;
		}
		if(!onCheckCode("#editFromCode")){
			check = false;
		}
		if(!onCheckExternalCode("#editFromExternalCode")){
			check = false;
		}
		if(!onCheckYears("#editFromYears")){
			check = false;
		}
		if(!onCheckRating("#editFromRating")){
			check = false;
		}
		if(!onCheckTime("#editFromStartTime")){
			check = false;
		}
		if(!onCheckTime("#editFromEndTime")){
			check = false;
		}
		return check;
	}
	/*************************************************字段校验******************************************************/
</script>